// pages/userInfoCenter/index.js
const App = getApp();
const { Dialog, Toast } = require("../../../../config/modal")
const utils = require("../../../../utils/util")
Page({
    /**
     * 页面的初始数据
     */
    data: {
        theamTextColor: App.globalData.theamTextColor,

        loadMore: {
            show: false,
            type: "loading",
            text: "加载中..."
        },

        address: {
            list: [],
        },
        page: {
            pageNum: 1,
            pageSize: 15,
            pages: 0,
        },
        // 标志点击地址是否返回，从积分商品来的就返回
        fromParams: {
            flag: false,
            goodsNo: ""
        }

    },
    URL: {
        getList: "/address/pageList",
        updateDefault: "/address/updateDefault",
        delete: "/address/remove/?",
    },
    page: {
        update: "/pages/tabbar/user/address/edit/index",
        userCenter: '/pages/tabbar/user/userInfoCenter/index'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.hideShareMenu();
        utils.saveUrlParams(this, options, [])
        utils.saveFromPage(App, getCurrentPages());
    },
    onShow() {
        this.init(this.data.page.pageNum, false);
    },

    // 点击设置默认
    setDefault(e) {
        let defaultVal = e.detail
        let id = e.currentTarget.dataset.id
        if (!defaultVal) {
            let list = this.data.address.list;
            for (const listKey in list) {
                let item = list[listKey];
                if (item.id = id && item.isDefault != defaultVal) {
                    Toast("已是默认地址")
                    return;
                }
            }
        }
        App.$post(true, this.URL.updateDefault, { id: id, isDefault: defaultVal }, res => {
            if (res.R) {
                this.onShow();
            }
        })
    },
    // 添加
    btnAddClick() {
        App.$navigateTo(this.page.update + utils.getFromUrlParams(this), true);
    },

    // 删除
    btnDelClick(e) {
        App.$alertConfirm({
            msg: "是否删除?",
            success: () => {
                let item = e.currentTarget.dataset.item;
                App.$delete(true, this.URL.delete.replace("?", item.id), {}, res => {
                    if (res.R) {
                        this.onShow();
                    }
                })
            }
        })
    }

    // 编辑
    , btnEditClick(e) {
        let item = e.currentTarget.dataset.item;
        App.$navigateTo(this.page.update + utils.getFromUrlParams(this) + "&item=" + JSON.stringify(item), true);
    },
    // 初始化地址列表
    init(pageNum, loadMore) {
        let params = {
            pageNum: pageNum,
            pageSize: this.data.page.pageSize,
        }
        this.setLoadingMore(loadMore, "loading", "加载中...")
        App.$get(!loadMore ? true : loadMore, this.URL.getList, params, res => {
            this.setData({
                "loadMore.show": false,
            })
            if (res.R) {
                this.setData({
                    "page.pageNum": res.data.pageNum,
                    "page.pageSize": res.data.pageSize,
                    "page.pages": res.data.pages
                })
                if (!loadMore) {
                    this.setData({
                        "address.list": res.data.rows,
                    })
                    return;
                }
                let sourceData = this.data.address.list;
                let newData = res.data.rows;
                newData.forEach(data => {
                    sourceData.push(data);
                })
                this.setData({
                    "address.list": sourceData,
                })
            }
        })
    },

    // 返回上一级
    goBack() {
        App.$redirectTo(this.page.userCenter, false);
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },


    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.data.page.pages === 0 || this.data.page.pageNum + 1 > this.data.page.pages) {
            this.setLoadingMore(true, "end", "我是用底线的~")
            return;
        }
        this.initIntegral(this.data.page.pageNum + 1, true);
    },
    setLoadingMore(show, type, text) {
        this.setData({
            "loadMore.show": show,
            "loadMore.type": type,
            "loadMore.text": text,
        })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})
